package com.hxkj.service.userCenter.impl;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.client.service.SysCooperationServiceClient;
import com.hxkj.domain.po.SysCooperation;
import com.hxkj.domain.vo.SysCooperationVO;
import com.hxkj.service.userCenter.SysCooperationService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SysCooperationServiceImpl implements SysCooperationService {

    @Autowired
    private SysCooperationServiceClient sysCooperationServiceClient;

    @Override
    public void addCooperation(SysCooperationVO sysCooperationVO) throws Exception {
        SysCooperationVO vo = sysCooperationVO;//JSONObject.toJavaObject(jsonObject, SysCooperationVO.class);
        SysCooperation code = new SysCooperation();
        code.setWechatUrl(vo.getWechatUrl());
        SysCooperation phone = new SysCooperation();
        phone.setTitle(vo.getTitle());
        phone.setPhone(vo.getPhone());
        phone.setContent(vo.getContent());
        List<SysCooperation> list = vo.getList();
        Map<String, Object> params = new HashMap<>();
        params.put("code",code);
        params.put("phone",phone);
        params.put("list",list);
        ResultMessageStruct result = sysCooperationServiceClient.addCooperation(params);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public void addQQ(SysCooperation sysCooperation) throws Exception {
        String type = sysCooperation.getCooperationType();
        if(StringUtils.isBlank(type)||!"3".equals(type)){
            throw new CustomMessageException("分类出错");
        }
        ResultMessageStruct result = sysCooperationServiceClient.addQQ(sysCooperation);
        if(result.getCode()!=200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public void modifyCooperation(SysCooperation sysCooperation) throws Exception{
        String type = sysCooperation.getCooperationType();
        if(StringUtils.isBlank(type)||!"3".equals(type)){
            throw new CustomMessageException("分类出错");
        }
        ResultMessageStruct result = sysCooperationServiceClient.modifyCooperation(sysCooperation);
        if(result.getCode()!=200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public SysCooperationVO getSysCooperation() throws Exception{
        ResultDataStruct<SysCooperationVO> result = sysCooperationServiceClient.getSysCooperation();
        if(result.getCode()!=200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }

    @Override
    public void deleteCooperation(SysCooperation sysCooperation) throws Exception{
        if(sysCooperation.getCooperationId() == null){
            throw new CustomMessageException("编号不能为空");
        }
        ResultMessageStruct result = sysCooperationServiceClient.deleteCooperation(sysCooperation);
        if(result.getCode()!=200){
            throw new CustomMessageException(result.getMessage());
        }
    }
}

